package com.ruoyi.project.mes.vehicleControl.service.impl;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.mes.vehicleControl.domain.QasKeyPartTrack;
import com.ruoyi.project.mes.vehicleControl.domain.vo.BindInVo;
import com.ruoyi.project.mes.vehicleControl.domain.vo.BindRecordInVo;
import com.ruoyi.project.mes.vehicleControl.domain.vo.BindRecordOutVo;
import com.ruoyi.project.mes.vehicleControl.domain.vo.KeyPartTypeBomVo;
import com.ruoyi.project.mes.vehicleControl.mapper.BatteryBindMapper;
import com.ruoyi.project.mes.vehicleControl.mapper.QasKeyPartTrackMapper;
import com.ruoyi.project.mes.vehicleControl.service.BatteryBindService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author : kyc
 * @Description: 电池包绑定
 * @date : 2021-03-15 10:18
 **/
@RequiredArgsConstructor
@Service
public class BatteryBindServiceImpl implements BatteryBindService {

    private final BatteryBindMapper batteryBindMapper;

    private final QasKeyPartTrackMapper qasKeyPartTrackMapper;

    @Override
    public BindRecordOutVo listBindRecord(BindRecordInVo inVo) {
        // 通过VIN在TM_OFM_ORDER订单表中找到对应的整车物料号
        String materialNo = batteryBindMapper.getMaterialNo(inVo.getVin());
        if (StrUtil.isBlank(materialNo)) {
            throw new CustomException("未找到对应vin号的物料号");
        }
        // 通过工位编号找到对应的工位id
        String ulocId = batteryBindMapper.getUlocId(inVo.getUlocNo());
        if (StrUtil.isBlank(ulocId)) {
            throw new CustomException("未找到对应工位");
        }
        // 通过工位id和物料号找到对应的所有追溯件清单
        List<String> list = batteryBindMapper.listKeyParts(materialNo, ulocId);
        // 查询已绑定的追溯件
        List<String> bindKeyParts = batteryBindMapper.listBindKeyParts(inVo.getVin(), ulocId);
        // 筛选未绑定关键件
        for (String bindKeyPart : bindKeyParts) {
            if (list.contains(bindKeyPart)) {
                list.remove(bindKeyPart);
            }
        }
        BindRecordOutVo outVo = new BindRecordOutVo();
        outVo.setBindKeyParts(bindKeyParts);
        outVo.setUnBindKeyParts(list);
        return outVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bind(BindInVo inVo) {
        // 电池包代码
        String partTraceNo = "66";
        // 查询是否存在该vin号对应的关键件配置
        Integer count = batteryBindMapper.existConfig(inVo.getVin(), partTraceNo);
        if (count == 0) {
            throw new CustomException("该vin号无对应配置");
        }
        // 查询是否已绑定过该关键件
        Integer count1 = batteryBindMapper.existKeyParts(inVo);
        if (count1 > 0) {
            throw new CustomException("该条码已被绑定");
        }
        // 校验序列号
        String sapFeatureCode = batteryBindMapper.checkScanTrackNo(inVo.getVin());
        if (StrUtil.isBlank(sapFeatureCode)) {
            throw new CustomException("vin号不正确");
        }
        String[] check = sapFeatureCode.split("&");
        String result = inVo.getScanTrackNo().substring(0, check[0].length());
        if (!result.equals(check[0]) || !check[1].equals(String.valueOf(inVo.getScanTrackNo().length()))) {
            throw new CustomException("安装序列号不正确,请检查");
        }
        // 绑定记录
        QasKeyPartTrack qasKeyPartTrack = new QasKeyPartTrack();
        qasKeyPartTrack.setVin(inVo.getVin());
        qasKeyPartTrack.setKeypartNo(inVo.getScanTrackNo());
        qasKeyPartTrack.setScanUsername(inVo.getUsername());
        qasKeyPartTrack.setScanTime(new Timestamp(System.currentTimeMillis()));
        // 查询工位id
        String ulocId = batteryBindMapper.getUlocId(inVo.getUlocNo());
        qasKeyPartTrack.setTmBasUlocId(ulocId);
        qasKeyPartTrack.setLastUpdateTime(new Timestamp(System.currentTimeMillis()));
        qasKeyPartTrack.setLastUpdateUsername(inVo.getUsername());
        qasKeyPartTrack.setPid(1);
        // 工厂id
        Integer plantId = batteryBindMapper.getPlantId();
        qasKeyPartTrack.setTmBasPlantId(plantId);
        // 物料号
        String materialNo = batteryBindMapper.getMaterialNo(inVo.getVin());
        qasKeyPartTrack.setMaterialNo(materialNo);
        qasKeyPartTrack.setScanType(0);
        // 查询数据
        KeyPartTypeBomVo vo = batteryBindMapper.getKeyPartTypeBom(materialNo,ulocId);
        qasKeyPartTrack.setPartNo(vo.getPartNo());
        qasKeyPartTrack.setDcjbj(vo.getDcjbj());
        qasKeyPartTrack.setQzhbj(vo.getQzhbj());
        qasKeyPartTrack.setPartTraceNo(vo.getPartTraceNo());
        qasKeyPartTrack.setGroupNumber(vo.getGroupNumber());
        qasKeyPartTrack.setDcjbh(vo.getDcjbh());
        qasKeyPartTrack.setHbjbj(vo.getHbjbj());
        qasKeyPartTrack.setPartName(vo.getPartName());
        // 供应商号
        String supplierNo = batteryBindMapper.getSupplierNo(inVo);
        qasKeyPartTrack.setSupplierNo(supplierNo);
        qasKeyPartTrackMapper.insert(qasKeyPartTrack);
        // 车辆信息绑定电池包
        batteryBindMapper.vehicleBindPackNo(inVo);
    }
}
